package com.industrial.loweff.controller;

import cn.hutool.core.util.ArrayUtil;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.core.web.page.TableDataInfo;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.loweff.domain.JcQueryParam;
import com.industrial.loweff.domain.LowEfficiencyBenefitEvaluation;
import com.industrial.loweff.domain.SysEnterDiagnosisInfo;
import com.industrial.loweff.service.IEnterpriseDiagnosisService;
import com.industrial.system.api.domain.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 *     企业评价诊断报告
 * </p>
 * @author SangGuangCan
 * @since 2024-03-07
 */
@RestController
@RequestMapping("/enter/dia")
public class EnterpriseDiagnosisController extends BaseController {

    @Autowired
    private IEnterpriseDiagnosisService enterpriseDiagnosisService;

    /**
     * <p>
     *     查询列表
     * </p>
     * @param jcQueryParam 查询参数
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(JcQueryParam jcQueryParam){

        LocalDate localDate = LocalDate.now();
        jcQueryParam.setYears(String.valueOf(localDate.getYear() - 1));

        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        Set<String> userRole = SecurityUtils.getLoginUser().getRoles();

        if(ArrayUtil.contains(userRole.toArray(),"x_01_011")){
            if(StringUtils.isBlank(jcQueryParam.getCity())){
                jcQueryParam.setCounty(sysUser.getDistrict());
            }else{
                jcQueryParam.setCounty(jcQueryParam.getCity());
                jcQueryParam.setCity(null);
            }
        }else if(ArrayUtil.contains(userRole.toArray(),"s_01_011")){
            if(StringUtils.isBlank(jcQueryParam.getCity())){
                jcQueryParam.setCity(sysUser.getCity());
            }else{
                if(StringUtils.endsWith(jcQueryParam.getCity(),"0000")){

                }else if(StringUtils.endsWith(jcQueryParam.getCity(),"00")){
                    jcQueryParam.setCity(jcQueryParam.getCity());
                }else {
                    jcQueryParam.setCounty(jcQueryParam.getCity());
                    jcQueryParam.setCity(null);
                }
            }
        }else if(ArrayUtil.contains(userRole.toArray(),"p_01_011")){
            if(StringUtils.isBlank(jcQueryParam.getCity())){
                jcQueryParam.setCity(sysUser.getCity());
            }else{
                if(StringUtils.endsWith(jcQueryParam.getCity(),"0000")){
                    jcQueryParam.setProvince(jcQueryParam.getCity());
                    jcQueryParam.setCity(null);
                }else if(StringUtils.endsWith(jcQueryParam.getCity(),"00")){
                    jcQueryParam.setCity(jcQueryParam.getCity());
                }else {
                    jcQueryParam.setCounty(jcQueryParam.getCity());
                    jcQueryParam.setCity(null);
                }
            }
        }else if(ArrayUtil.contains(userRole.toArray(),"q_01_001")){
            String userName = sysUser.getUserName();
            jcQueryParam.setUscc(userName);
        }
        startPage();
        List<LowEfficiencyBenefitEvaluation> list = enterpriseDiagnosisService.list(jcQueryParam);
        return getDataTable(list);
    }

    /**
     * <p>
     *     查询企业指标值
     * </p>
     * @param jcQueryParam
     * @return
     */
    @GetMapping("/queryIndexData")
    public AjaxResult queryIndexData(JcQueryParam jcQueryParam){
        List<Map<String, String>> maps = enterpriseDiagnosisService.queryIndexData(jcQueryParam);
        return AjaxResult.success(maps);
    }

    /**
     * <p>
     *     近5年单位用地税收趋势分析
     * </p>
     */
    @GetMapping("/ydssQsCharts")
    public AjaxResult ydssQsCharts(JcQueryParam jcQueryParam){
        Map<String, Object> resultMap = enterpriseDiagnosisService.ydssQsCharts(jcQueryParam);
        return AjaxResult.success(resultMap);
    }

    /**
     * <p>
     *     近5年单位用地销售收入趋势分析
     * </p>
     */
    @GetMapping("/ydxsQsCharts")
    public AjaxResult ydxsQsCharts(JcQueryParam jcQueryParam){
        Map<String, Object> resultMap = enterpriseDiagnosisService.ydxsQsCharts(jcQueryParam);
        return AjaxResult.success(resultMap);
    }

    /**
     * <p>
     *     近5年单位能耗销售收入趋势分析
     * </p>
     */
    @GetMapping("/nhxsQsCharts")
    public AjaxResult nhxsQsCharts(JcQueryParam jcQueryParam){
        Map<String, Object> resultMap = enterpriseDiagnosisService.nhxsQsCharts(jcQueryParam);
        return AjaxResult.success(resultMap);
    }

    /**
     * <p>
     *     近5年单位排放销售收入趋势分析
     * </p>
     */
    @GetMapping("/pfxsQsCharts")
    public AjaxResult pfxsQsCharts(JcQueryParam jcQueryParam){
        Map<String, Object> resultMap = enterpriseDiagnosisService.pfxsQsCharts(jcQueryParam);
        return AjaxResult.success(resultMap);
    }

    /**
     * <p>
     *     近5年研发投入强度趋势分析
     * </p>
     */
    @GetMapping("/yftrQsCharts")
    public AjaxResult yftrQsCharts(JcQueryParam jcQueryParam){
        Map<String, Object> resultMap = enterpriseDiagnosisService.yftrQsCharts(jcQueryParam);
        return AjaxResult.success(resultMap);
    }

    /**
     * <p>
     *     近5年全员劳动生产率趋势分析
     * </p>
     */
    @GetMapping("/qyldQsCharts")
    public AjaxResult qyldQsCharts(JcQueryParam jcQueryParam){
        Map<String, Object> resultMap = enterpriseDiagnosisService.qyldQsCharts(jcQueryParam);
        return AjaxResult.success(resultMap);
    }

    /**
     * 发送报告给企业
     * @param params
     * @return
     */
    @PostMapping("/sendReport")
    public AjaxResult sendReport(@RequestBody SysEnterDiagnosisInfo params){
        enterpriseDiagnosisService.sendReport(params);
        return AjaxResult.success();
    }

    /**
     * 查询建义
     * @param jcQueryParam
     * @return
     */
    @GetMapping("/queryJy")
    public AjaxResult queryJy(JcQueryParam jcQueryParam){
        SysEnterDiagnosisInfo sysEnterDiagnosisInfo = enterpriseDiagnosisService.queryJy(jcQueryParam);
        return AjaxResult.success(sysEnterDiagnosisInfo);
    }
}
